﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

namespace SharpSoft.WPF.Controls
{
    public class HandlerThumb : Thumb
    {
        static HandlerThumb()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HandlerThumb), new FrameworkPropertyMetadata(typeof(HandlerThumb)));
        }
        public HandlerThumb()
        {
            this.Template = null;
            this.Rotate = new RotateTransform(0);
            this.SizeChanged += HandlerThumb_SizeChanged;
        }

        private void HandlerThumb_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.InvalidateVisual();
        }

        /// <summary>
        /// 控件的中心位置
        /// </summary>
        public Point Center
        {
            get { return (Point)GetValue(CenterProperty); }
            set { SetValue(CenterProperty, value); }
        }

        public static readonly DependencyProperty CenterProperty =
            DependencyProperty.Register("Center", typeof(Point), typeof(HandlerThumb), new PropertyMetadata(default(Point)));

        public HandlerThumbShape Shape { get; set; } = HandlerThumbShape.Rhombus;
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == CenterProperty)
            {
                reCaclLeftTop();
            }
        }
        /// <summary>
        /// 当前手柄的旋转
        /// </summary>
        internal RotateTransform Rotate { get; set; }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            reCaclLeftTop();

        }

        private void reCaclLeftTop()
        {
            Canvas.SetLeft(this, this.Center.X - this.ActualWidth / 2);
            Canvas.SetTop(this, this.Center.Y - this.ActualHeight / 2);

            this.Rotate.CenterX = this.ActualWidth / 2;
            this.Rotate.CenterY = this.ActualHeight / 2;
        }


        protected override void OnRender(DrawingContext dc)
        {
            //base.OnRender(dc);
            if (this.Rotate != null)
            {
                dc.PushTransform(this.Rotate);
            }
            var pen = new Pen(this.BorderBrush, this.BorderThickness.Left);
            var w = this.ActualWidth;
            var h = this.ActualHeight;
            var hw = this.ActualWidth / 2;
            var hh = this.ActualHeight / 2;
            switch (this.Shape)
            {
                case HandlerThumbShape.Square:
                    dc.DrawRectangle(this.Background, pen, new Rect(0, 0, w, h));
                    break;
                case HandlerThumbShape.Circle:
                    dc.DrawEllipse(this.Background, pen, new Point(hw, hh), hw, hh);
                    break;
                case HandlerThumbShape.Rhombus:
                    {
                        var p1 = new Point(hw, 0);
                        var p2 = new Point(w, hh);
                        var p3 = new Point(hw, h);
                        var p4 = new Point(0, hh);

                        PathGeometry ph = new PathGeometry();
                        PathFigure pf = new PathFigure();
                        ph.Figures.Add(pf);
                        pf.StartPoint = p1;
                        pf.Segments.Add(new LineSegment(p2, true));
                        pf.Segments.Add(new LineSegment(p3, true));
                        pf.Segments.Add(new LineSegment(p4, true));
                        pf.IsClosed = true;
                        dc.DrawGeometry(this.Background, pen, ph);
                    }
                    break;
                case HandlerThumbShape.Triangles:
                    {
                        var p1 = new Point(0, 0);
                        var p2 = new Point(w, hh);
                        var p3 = new Point(0, h);

                        PathGeometry ph = new PathGeometry();
                        PathFigure pf = new PathFigure();
                        ph.Figures.Add(pf);
                        pf.StartPoint = p1;
                        pf.Segments.Add(new LineSegment(p2, true));
                        pf.Segments.Add(new LineSegment(p3, true));
                        pf.IsClosed = true;
                        dc.DrawGeometry(this.Background, pen, ph);
                    }
                    break;
                default:
                    break;
            }
            if (this.Rotate != null)
            {
                dc.Pop();
            }
        }

    }
    public enum HandlerThumbShape
    {
        /// <summary>
        /// 方形
        /// </summary>
        Square,
        /// <summary>
        /// 圆形
        /// </summary>
        Circle,
        /// <summary>
        /// 菱形
        /// </summary>
        Rhombus,
        /// <summary>
        /// 三角形
        /// </summary>
        Triangles
    }
}
